package top.yamhk.agent.entity.dto;

import cn.hutool.json.JSONUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import top.yamhk.agent.entity.request.Request;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * @author yamhk
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@ToString
public class RequestCustomer extends Request implements Comparable, Serializable {
    private String ok;
    private String ng;
    private String order;
    private String beanName;
    private String clazz;
    private String methodName;
    private String method;
    private String args;
    private List<Class> argsType;
    private Map<String, String> requestHeader;

    public static String request(String clazz, String method, String args) {
        RequestCustomer request = new RequestCustomer();
        request.setClazz(clazz);
        request.setMethod(method);
        request.setArgs(args);
        return JSONUtil.toJsonStr(request);
    }

    public static String getArg(String finder) {
        try {
            return URLDecoder.decode(JSONUtil.toBean(finder, RequestCustomer.class).getArgs(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof RequestCustomer) {
            RequestCustomer requestCustomer = (RequestCustomer) o;
            if (!this.getClazz().equals(requestCustomer.getClazz())) {
                return this.getClazz().compareTo(requestCustomer.getClazz());
            }
            if (!this.getMethodName().equals(requestCustomer.getMethodName())) {
                return this.getMethodName().compareTo(requestCustomer.getMethodName());
            }
            return 0;
        } else {
            return 0;
        }
    }
}

